package model

import (
	"gorm.io/gorm"
	"shop/database/mysql"
)

// 分类表
type Types struct {
	gorm.Model
	Icon     string `gorm:"column:icon;type:varchar(255);comment:分类图片"  json:"icon"`
	Lv       int64  `gorm:"column:lv;type:tinyint(1);comment:分类等级"      json:"lv"`
	Pid      int64  `gorm:"column:pid;type:int(11);comment:父级id"         json:"pid"`
	Name     string `gorm:"column:name;type:varchar(100);comment:分类名称"  json:"name"`
	Location int64  `gorm:"type:tinyint(2);comment:1:分类1;2分类2;0全部分类" json:"location"`
}

// GoodsInfo 根据分类 ID 列表查询分类信息
func (t *Types) SearchBYID(typeIDs []int64) (types *Types, err error) {
	return types, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(t).Where("id in(?)", typeIDs).Find(&types).Error
	})
}

// 查询分类等级详情
func (t *Types) GoodsTypeInfoByLv(location int64) (info *[]Types, err error) {
	return info, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(t).Where("location = ?", location).Find(&info).Error
	})
}

// GoodsInfoByPID 根据父级分类 ID 列表查询分类信息
func (t *Types) SearchByIDs(pids []int64) (info []*Types, err error) {
	return info, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(t).Where("pid in(?)", pids).Find(&info).Error
	})

}

type GoodsTypes struct {
	ID         int64  `json:"id"`
	Icon       string `json:"icon"`
	Lv         int    `json:"lv"`
	Pid        int64  `json:"pid"`
	Name       string `json:"name"`
	Location   int    `json:"location"`
	GoodsTypes []GoodsTypes
}

// 商品分类
func (t *Types) GoodsTypes() (goodsTypes []GoodsTypes, err error) {
	return goodsTypes, mysql.WithMysql(func(db *gorm.DB) error {
		var typesGoods []Types
		if err = db.Find(&typesGoods).Error; err != nil {
			return err
		}

		// 构建一级分类
		for _, goodsType := range typesGoods {
			if goodsType.Lv == 1 {
				firstLevel := GoodsTypes{
					ID:         int64(goodsType.ID),
					Icon:       goodsType.Icon,
					Lv:         int(goodsType.Lv),
					Pid:        goodsType.Pid,
					Name:       goodsType.Name,
					Location:   int(goodsType.Location),
					GoodsTypes: []GoodsTypes{},
				}
				// 构建二级分类
				for _, subGoodsType := range typesGoods {
					if subGoodsType.Lv == 2 && subGoodsType.Pid == int64(goodsType.ID) {
						secondLevel := GoodsTypes{
							ID:         int64(goodsType.ID),
							Icon:       subGoodsType.Icon,
							Lv:         int(subGoodsType.Lv),
							Pid:        subGoodsType.Pid,
							Name:       subGoodsType.Name,
							Location:   int(subGoodsType.Location),
							GoodsTypes: []GoodsTypes{},
						}
						// 构建三级分类
						for _, subSubGoodsType := range typesGoods {
							if subSubGoodsType.Lv == 3 && subSubGoodsType.Pid == int64(subGoodsType.ID) {
								thirdLevel := GoodsTypes{
									ID:         int64(goodsType.ID),
									Icon:       subSubGoodsType.Icon,
									Lv:         int(subSubGoodsType.Lv),
									Pid:        subSubGoodsType.Pid,
									Name:       subSubGoodsType.Name,
									Location:   int(subSubGoodsType.Location),
									GoodsTypes: []GoodsTypes{},
								}
								secondLevel.GoodsTypes = append(secondLevel.GoodsTypes, thirdLevel)
							}
						}
						firstLevel.GoodsTypes = append(firstLevel.GoodsTypes, secondLevel)
					}
				}
				goodsTypes = append(goodsTypes, firstLevel)
			}
		}
		return err
	})
}
